package apy.codecase.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import apy.codecase.App;

/**
 * Created by Administrator on 2017/10/18.
 */

public class ASPUtils {
    private final static String name = "config";
    private final static int mode = Context.MODE_PRIVATE;

    /**
     * 保存首选项
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveBoolean(Context context, String key, boolean value) {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.apply();
    }

    public static void saveBoolean(String key) {
        SharedPreferences sp = UIUtils.getContext().getSharedPreferences(name, mode);
        Editor edit = sp.edit();
        edit.putBoolean(key, true);
        edit.apply();
    }

    public static void remove(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        sp.edit().remove(key).apply();
    }

    public static void saveInt(Context context, String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        Editor edit = sp.edit();
        edit.putInt(key, value);
        edit.apply();
    }

    public static void saveString(String key, String value) {
        SharedPreferences sp = UIUtils.getContext().getSharedPreferences(name, mode);
        Editor edit = sp.edit();
        edit.putString(key, value);
        edit.apply();
    }

    public static String getString(String key) {
        SharedPreferences sp = UIUtils.getContext().getSharedPreferences(name, mode);
        return sp.getString(key, "");
    }

    public static String getStringForResult(String key) {
        SharedPreferences sp = UIUtils.getContext().getSharedPreferences(name, mode);
        return sp.getString(key, "0");
    }

    /**
     * 获取首选项
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static boolean getBoolean(Context context, String key, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        return sp.getBoolean(key, defValue);
    }

    public static boolean getBoolean(String key) {
        SharedPreferences sp = UIUtils.getContext().getSharedPreferences(name, mode);
        return sp.getBoolean(key, false);
    }

    public static int getInt(Context context, String key, int defValue) {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        return sp.getInt(key, defValue);
    }

    public static String getString(Context context, String key, String defValue) {
        SharedPreferences sp = context.getSharedPreferences(name, mode);
        return sp.getString(key, defValue);
    }

    public static String getString(String key, String defValue) {
        SharedPreferences sp = App.getContext().getSharedPreferences(name, mode);
        return sp.getString(key, defValue);
    }


    public static void clear(){
        SharedPreferences sp = UIUtils.getContext().getSharedPreferences("config",0);
        sp.edit().clear().commit();
    }
}
